home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
WASTE Object Handlers 1.2.6.sit
/
WASTE Object Handlers 1.2.6
/
Sample Code
/
DragTextFile.c
Wrap
Text File
|
1996-07-16
|
5KB
|
181 lines
/*
This is an example of how to drag text files to a WASTE instance and
have them imported. For this example to work, you must be using the
'hfs ' type WASTE object supplied with this package.
28 March 1996, compatability updates made for new Universal Headers, WASTE 1.2a5
and pre-Copland accessor routines
Michael F. Kamprath
kamprath@kagi.com
19 March 1995
*/
#ifndef __DRAG__
#include <Drag.h>
#endif
#ifndef _WASTE_
#include "WASTE.h"
#endif
pascal OSErr MyWEReceiveHandler( WindowRef theWindow,
Ptr handlerRefCon,
DragReference theDrag)
{
ItemReference theItem;
Size itemSize;
Ptr textP;
StScrpHandle styleH;
WESoupHandle soupH;
GrafPtr savePort;
DocPtr theDoc;
HFSFlavor **theHFS;
long insertionOffset, oldNLines, curEOF;
unsigned short items, index;
short fRefNum;
OSErr iErr;
Point zeroPoint = {0, 0};
Point mouse;
LongPt dropLocation;
char dropEdge;
if (theWindow != nil)
{
GetPort(&savePort);
SetPortWindowPort(theWindow);
theDoc = (DocPtr)GetWRefCon( theWindow ); // Or recover your document or
// WASTE handle how ever you see
// fit.
oldNLines = WECountLines( theDoc->theWE );
CountDragItems(theDrag, &items);
for (index = 1; index <= items; index++) // step through each drag item
// and check for text files
{
GetDragItemReferenceNumber(theDrag, index, &theItem);
if (!(iErr = GetFlavorDataSize(theDrag, theItem, flavorTypeHFS, &itemSize)))
{
// Found a text file. Now import
// it to the WASTE instance;
theHFS = (HFSFlavor **)NewHandleClear(sizeof(HFSFlavor));
if (theHFS)
{
HLockHi( (Handle)theHFS );
GetFlavorData( theDrag, theItem, flavorTypeHFS, *theHFS, &itemSize, 0L);
if (( (*theHFS)->fileType == 'TEXT' )||( (*theHFS)->fileType == 'ttro' ))
{
// Load the text file into memory
iErr = FSpOpenDF( &(*theHFS)->fileSpec, fsCurPerm, &fRefNum );
iErr = GetEOF(fRefNum,&curEOF);
textP = NewPtrClear( curEOF );
iErr = SetFPos(fRefNum,fsFromStart,0);
iErr = FSRead( fRefNum, &curEOF, textP);
iErr = FSClose( fRefNum );
fRefNum = FSpOpenResFile(&(*theHFS)->fileSpec,fsCurPerm);
if ( fRefNum != -1 )
{
// has resource fork, so look for style and soup
styleH = (StScrpHandle)Get1Resource( 'styl', 128 );
iErr = ResError()
if (iErr)
styleH = nil;
else
DetachResource( (Handle)styleH );
soupH = (WESoupHandle)Get1Resource( 'SOUP', 128 );
iErr = ResError()
if (iErr)
soupH = nil;
else
DetachResource( (Handle)soupH );
CloseResFile(fRefNum);
}
else
{
// does not have resource fork, so no style or soup.
styleH = nil;
soupH = nil;
}
// insert the text file into the current position
iErr = GetDragMouse(theDrag, &mouse, &zeroPoint);
if (iErr != noErr)
goto cleanup;
GlobalToLocal(&mouse);
WEPointToLongPoint(mouse, &dropLocation);
insertionOffset = WEGetOffset(&dropLocation, &dropEdge, theDoc->theWE);
WESetSelection( insertionOffset, insertionOffset, theDoc->theWE );
iErr = WEInsert(textP, curEOF, styleH, soupH, theDoc->theWE);
WESetSelection( insertionOffset, insertionOffset+curEOF, theDoc->theWE );
// dispose of memory
cleanup:
HUnlock( (Handle)theHFS );
DisposeHandle( (Handle)theHFS );
DisposePtr( textP );
if (styleH) DisposeHandle( (Handle)styleH );
if (soupH) DisposeHandle( (Handle)soupH );
// update the WASTE instance if need be
//
// your program will probably do ssomething different
if ( WECountLines( theDoc->theWE ) != oldNLines )
SetVScroll(theDoc);
theDoc->dirty = true;
SetPort(savePort);
return (iErr);
}
HUnlock( (Handle)theHFS );
DisposeHandle( (Handle)theHFS );
}
}
// If the code got to here, that means there was no
// text file item in the drag. Let WASTE handle the drag then.
HideDragHilite(theDrag); // I found I need to do this for cosmetic purposes sometimes
iErr = WEReceiveDrag(theDrag, theDoc->theWE );
// update the WASTE instance if need be
//
// your program will probably do ssomething different
if ( WECountLines( theDoc->theWE ) != oldNLines )
SetVScroll(theDoc);
theDoc->dirty = true;
}
else
iErr = dragNotAcceptedErr;
SetPort(savePort);
}
else
iErr = noErr;
return(noErr);
}